#ifndef __MY_CONFIG__
#define __MY_CONFIG__

#include <mutex>
#include <string>
#include "Util.hpp"
#include <jsoncpp/json/json.h>
const std::string CONFIG_FILE = "./cloud.conf";
namespace cloud
{
    class Config
    {
    public:
        static Config *GetInstance()
        {
            if (_inst == nullptr)
            {
                _mutex.lock();
                if (_inst == nullptr)
                {
                    _inst = new Config;
                }
                _mutex.unlock();
            }

            return _inst;
        }
        int GetHotTime()
        {
            return _hot_time;
        }
        int GetServerPort()
        {
            return _server_port;
        }
        std::string GetServerIp()
        {
            return _server_ip;
        }
        std::string GetDownloadPrefix()
        {
            return _download_prefix;
        }
        std::string GetPackfileSuffix()
        {
            return _packfile_suffix;
        }
        std::string GetBackDir()
        {
            return _back_dir;
        }
        std::string GetPackDir()
        {
            return _pack_dir;
        }
        std::string GetBackupFile()
        {
            return _backup_file;
        }

    private:
        Config()
        {
            ReadConfFile();
        }
        Config(const Config &cf) = delete;
        Config &operator=(const Config &cf) = delete;
        bool ReadConfFile()
        {
            FileUtil fu(CONFIG_FILE);
            std::string body;
            if (fu.GetContent(&body) == false)
            {
                std::cout << "read conf.file fail" << std::endl;
                return false;
            }
            Json::Value root;
            if (JsonUtil::UnSerialize(body, &root) == false)
            {
                std::cout << "parase fail" << std::endl;
                return false;
            }
            _hot_time = root["hot_time"].asInt();
            _server_port = root["server_port"].asInt();
            _server_ip = root["server_ip"].asString();
            _download_prefix = root["download_prefix"].asString();
            _packfile_suffix = root["packfile_suffix"].asString();
            _back_dir = root["back_dir"].asString();
            _pack_dir = root["pack_dir"].asString();
            _backup_file = root["backup_file"].asString();
            return true;
        }

    private:
        int _hot_time;
        int _server_port;
        std::string _server_ip;
        std::string _download_prefix;
        std::string _packfile_suffix;
        std::string _back_dir;
        std::string _pack_dir;
        std::string _backup_file;
        static Config *_inst;
        static std::mutex _mutex;
    };
    Config *Config::_inst = nullptr;
    std::mutex Config::_mutex;
}

#endif